﻿using HtmlAgilityPack;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WeiXin
{
    public class GroupInfo
    {
        public int count { get; set; }
        public string groupName { get; set; }
        public string groupid { get; set; }
    }

    public class GroupInfoList
    {
        public GroupInfoList()
        {
            data = new List<GroupInfo>();
        }
        public List<GroupInfo> data;
    }

    public static class WxMessageHelper
    {

        public static string GetChatRoom(string html)
        {
            string groupId = "";
            Regex regGroupId = new Regex(@"\[群-->\]\d+@chatroom");
            Match mGroupId = regGroupId.Match(html);
            if (mGroupId.Success)
            {
                groupId = mGroupId.Value.Replace("[群-->]", "").Replace(" ", "");
            }
            return groupId;
        }

        public static string GetGroupName(string html)
        {
            string groupName = "";
            Regex regGroupName = new Regex(@"chatroom\|\|[^||]*\|\|");
            Match mGroupName = regGroupName.Match(html);
            if (mGroupName.Success)
            {
                string sdf2 = mGroupName.Value;
                string[] cc = sdf2.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                groupName = cc[1];
            }
            return groupName;
        }


        //发送人昵称
        public static string GetNickName(string html)
        {
            string nickName = "";

            Regex regNickName = new Regex(@"\[发信人-->\]\S+\[消息内容-->\]");
            Match mNickName = regNickName.Match(html);
            if (mNickName.Success)
            {
                string nickMsg = mNickName.Value;
                string[] arrNick = nickMsg.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                if (arrNick.Count() == 3)
                {
                    nickName = arrNick[1];
                }
            }
            return nickName;
        }

        public static string GetWxId(string html)
        {
            string wxId = "";

            Regex regNickName = new Regex(@"\[发信人-->\]\S+\[消息内容-->\]");
            Match mNickName = regNickName.Match(html);
            if (mNickName.Success)
            {
                string nickMsg = mNickName.Value;
                string[] arrNick = nickMsg.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                if (arrNick.Count() == 3)
                {
                    wxId = arrNick[0];
                }
            }
            return wxId.Replace("[发信人-->]", "");
        }


        public static string GetWxCardJson(string html)
        {
            Regex regCard = new Regex(@"\[消息内容-->\]([\s\S]*)\[校验消息-->\]");
            Match mCard = regCard.Match(html);
            if (mCard.Success)
            {
                string str = mCard.Value;

                var model = new
                {
                    bigheadimgurl = GetTagAttributes(str, "msg", "bigheadimgurl"),
                    nickname = GetTagAttributes(str, "msg", "nickname"),
                    province = GetTagAttributes(str, "msg", "province"),
                    sign = GetTagAttributes(str, "msg", "sign"),
                    sex = GetTagAttributes(str, "msg", "sex")
                };
                return JsonConvert.SerializeObject(model);
            }
            return "";
        }


        public static string GetTagAttributes(string html, string tag, string attr)
        {
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);

            List<string> list = new List<string>();
            foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//" + tag + "[@" + attr + "]"))
            {
                string v = node.GetAttributeValue(attr, "");
                if (!string.IsNullOrEmpty(v))
                {
                    list.Add(v);
                }
            }
            if (list.Count > 0)
            {
                return list[0];
            }
            return "";
        }
    }
}
